#include "test.h"

#if (UNIT_TEST_ENABLE==1)
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;

void ut_uart(void)
{
	uint8_t rx1bf[64];
	uint8_t tx1bf[64];
	uint8_t rx2bf[64];
	uint8_t tx2bf[64];

	while(1){
		sprintf((char *)tx1bf, "test uart #%d\r\n", 1);
		HAL_UART_Transmit(&huart1, tx1bf, 64, 0xff);
		HAL_Delay(1000);

		sprintf((char *)tx2bf, "test uart #%d\r\n", 2);
		HAL_UART_Transmit(&huart2, tx2bf, 64, 0xff);
		HAL_Delay(1000);
	}
}

void ut_printf_uart1(void)
{
	while (1)
	{
		printf("test printf uart1\r\n");
		HAL_Delay(500);
	}
	
}

void ut_gpio(void)
{
	while (1)
	{
		HAL_GPIO_TogglePin(DAC_CS_GPIO_Port, DAC_CS_Pin);
		HAL_GPIO_TogglePin(LED_CC_GPIO_Port, LED_CC_Pin);
		HAL_GPIO_TogglePin(LED_CV_GPIO_Port, LED_CV_Pin);
		HAL_GPIO_TogglePin(LED_PWM_GPIO_Port, LED_PWM_Pin);
		HAL_GPIO_TogglePin(OLED_CS_GPIO_Port, OLED_CS_Pin);
		HAL_GPIO_TogglePin(OLED_DC_GPIO_Port, OLED_DC_Pin);
		HAL_GPIO_TogglePin(OLED_RES_GPIO_Port, OLED_RES_Pin);
		HAL_Delay(10);	
	}
}

extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi2;

void dacSetCs(uint8_t val)
{
	HAL_GPIO_WritePin(DAC_CS_GPIO_Port, DAC_CS_Pin, val? GPIO_PIN_SET: GPIO_PIN_RESET);
}

void dacWriteSpi16(uint16_t val)
{
	static uint8_t arr[2];
	
	arr[0] = val >> 8;
	arr[1] = val & 0xFF;

	HAL_SPI_Transmit(&hspi1, (uint8_t*)&arr, 2, 0xff);
}



void ut_tlv5618(void)
{
	struct Tlv5618 dac;
	tlv5618_init(&dac, dacSetCs, dacWriteSpi16);

	while (1)
	{
		tlv5618_setOutA(&dac, 4096*2/5);
		tlv5618_setOutB(&dac, 4096*2/5);
		HAL_Delay(1500);

		tlv5618_setOutA(&dac, 4096*4/5);
		tlv5618_setOutB(&dac, 4096*4/5);
		HAL_Delay(1500);	
	}
	
	
}

void ut_bsp_dac(void)
{
	printf("Bsp Dac Test Start\r\n");

	bsp_dacInitial();
	HAL_Delay(100);

	bsp_setDacCV(0.15);
	HAL_Delay(100);

	bsp_setDacCC(0.25);
	HAL_Delay(100);
	
	while (1)
	{
		//bsp_setDacCC(0.5);
		bsp_setDacCV(0.5);
		HAL_Delay(1500);

		//bsp_setDacCC(1.0);
		bsp_setDacCV(1.0);
		HAL_Delay(1500);	
	}
}


void uartParse(void)
{
	uint8_t rxbuffer[64] = {0};
	
}

void ut_test(void)
{
	//ut_uart();
	//ut_printf_uart1();
	//ut_gpio();
	//ut_tlv5618();
	//ut_bsp_dac();

	uartParse();
}

#endif //UNIT_TEST_ENABLE
